// pages/wallet/wallet.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    active:0,
    walletLeft:"--",
    article_id:0,
    list:[]
  },
  onShow() {
		this.load();
	},
  async load(){
    let info=await wx.util.post("wallet/index",{},false);
    this.setData({
      list:info.data.list,
      walletLeft:info.data.wallet_left,
      article_id:info.data.article_id
    })
  },
  async subAc(){
    let res=await wx.util.post("wallet/charge",this.data.list[this.data.active],false);
    await this.pay(res.data.id);
  },
  async pay(id){
    let that=this;
    let res=await wx.util.post("wallet/weixin_jsapi",{id:id},false);
    wx.requestPayment({
      timeStamp: res.data.timeStamp,
      nonceStr: res.data.nonceStr,
      package: res.data.package,
      signType: res.data.signType,
      paySign: res.data.paySign,
      success (res) {
        wx.util.toast('支付成功');
        that.load();
      },
      fail (res) {
        // wx.util.toast(res.errMsg);
      }
      })
  },
  setPrice(e) {
    let index=e.currentTarget.dataset.index;
    this.setData({
      active:index
    })
  },
  protocol(){
    wx.navigateTo({
      url:'/pages/articleDetail/articleDetail?id='+this.data.article_id
    })
  },
  log(){
    wx.navigateTo({
      url:'/pages/wallet/log'
    })
  }
})